package com.uniknow.app.operate.impl;

import java.util.List;

import android.content.Context;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.uniknow.app.AppConfig;
import com.uniknow.app.bin.NewsBean;
import com.uniknow.app.bin.ResponseBean;
import com.uniknow.app.bin.UniChannelBean;
import com.uniknow.app.bin.UserBean;
import com.uniknow.app.bin.WixAccountBean;
import com.uniknow.app.http.AsyncHttpResponseHandler;
import com.uniknow.app.http.RequestParams;
import com.uniknow.app.operate.BaseListener;
import com.uniknow.app.operate.CommonOperate;
import com.uniknow.app.operate.HttpPath;
import com.uniknow.app.util.SynchttpClient;
import com.uniknow.app.util.Sysout;
/**
 * 公共网络访问操作类
 * @author lianjiao
 */
public class CommonOperateImpl implements CommonOperate {
	private static CommonOperateImpl instance = null;
	private Context mContext;
	
	public static CommonOperateImpl getOperate(Context context){
		if(instance==null){
			instance = new CommonOperateImpl(context);
		}
		return instance;
	}
	
	private CommonOperateImpl(Context context){
		this.mContext = context;
	}

	
	private SynchttpClient getClient(){
		return SynchttpClient.getClient();
	}
	
	@Override
	public void getNews(UserBean user,int pageNumber,int pageSize, final BaseListener listener) {
		RequestParams params = new RequestParams();
		params.put("user.id", user.getUserid()+"");
		params.put("pager.offset", pageNumber+"");
		getClient().post(HttpPath.GETNEWS, params, new AsyncHttpResponseHandler(listener){
			
			@Override
			public void onSuccess(String content) {	
				Gson gson = new Gson();
				ResponseBean rs = (ResponseBean) gson.fromJson(content, ResponseBean.class);
				if (rs.OK()) {
					List<NewsBean> retList = gson.fromJson(gson.toJson(rs.getContent()),new TypeToken<List<NewsBean>>() {}.getType());
					listener.onSuccess(retList);
				}else{
					listener.onError(rs.errorInfo());
				}
			}
			
		});
		
	}

	@Override
	public void sendNews(UserBean user,NewsBean newsBean, BaseListener listener) {
		newsBean.setUserid(user.getUserid());
		newsBean.setAuthor(user.getUserName());
		Gson gson = new Gson();
		String jsonParams = gson.toJson(newsBean);
		Sysout.i("jsonParams = "+jsonParams);
		RequestParams params = new RequestParams();
		params.put("user.id", user.getUserid()+"");
		params.put("jsonParams", jsonParams);
		getClient().post(HttpPath.SENDNEWS, params, new AsyncHttpResponseHandler(listener){
			
			@Override
			public void onSuccess(String content) {	
				super.onSuccess(content);
			}
			
		});
		
	}

	@Override
	public void addWixAccount(UserBean user, WixAccountBean wixAccountBean,
			BaseListener listener) {
		RequestParams params = new RequestParams();
		params.put("user.id", user.getUserid()+"");
		params.put("wixBeans[0].dsc", wixAccountBean.getDsc());
		params.put("wixBeans[0].appid", wixAccountBean.getAppid());
		params.put("wixBeans[0].appkey", wixAccountBean.getAppkey());
		params.put("wixBeans[0].token", wixAccountBean.getApptoken());
		params.put("wixBeans[0].appurl", wixAccountBean.getAppurl());
		
		getClient().post(HttpPath.ADD_WIXACCOUNT, params, new AsyncHttpResponseHandler(listener){
			
			@Override
			public void onSuccess(String content) {	
				super.onSuccess(content);
			}
			
		});
		
	}

	@Override
	public void getWixAccount(UserBean user, final BaseListener listener) {
		RequestParams params = new RequestParams();
		params.put("user.id", user.getUserid()+"");
		
		getClient().post(HttpPath.GET_WIXACCOUNT, params, new AsyncHttpResponseHandler(listener){
			
			@Override
			public void onSuccess(String content) {	
				//super.onSuccess(content);
				Gson gson = new Gson();
				ResponseBean rs = (ResponseBean) gson.fromJson(content, ResponseBean.class);
				if (rs.OK()) {
					List<WixAccountBean> retList = gson.fromJson(gson.toJson(rs.getContent()),new TypeToken<List<WixAccountBean>>() {}.getType());
					listener.onSuccess(retList);
				}else{
					listener.onError(rs.errorInfo());
				}
			}
			
		});
		
	}

	@Override
	public void publicNews(UserBean user, NewsBean newsBean,
			List<UniChannelBean> listChannel, BaseListener listener) {
		newsBean.setUserid(user.getUserid());
		if(AppConfig.DeBug){
			newsBean.setUserid(16);
		}
		newsBean.setAuthor(user.getUserName());
		Gson gson = new Gson();
		JsonObject jo = new JsonObject();
		jo.addProperty("news", gson.toJson(newsBean));
		jo.addProperty("listChannel", gson.toJson(listChannel));
		String jsonParams = gson.toJson(jo);
		Sysout.i("jsonParams = "+jsonParams);
		RequestParams params = new RequestParams();
		params.put("user.id", user.getUserid()+"");
		params.put("jsonParams", jsonParams);
		getClient().post(HttpPath.PUBLICNEWS, params, new AsyncHttpResponseHandler(listener){
			
			@Override
			public void onSuccess(String content) {	
				super.onSuccess(content);
			}
			
		});
		
	}
	
	

	
}
